Buildstructor
Derive a builder from constructors/methods using the typesafe builder pattern!
Use this if your constructor/method has:
- Optional parameters.
- A large number of parameters.
- Collections parameters.
Installation
Add the dependency to your Cargo.toml
[]
= "*"
Usage / Example
- Annotate your
impl
with#[buildstructor::buildstructor]
. - Annotate your
fn
with#[builder]
. - Use your automatically derived builder.
Derive usage
For simple usage a default constructor and builder may be adequate.
Use #[derive(buildstructor::Builder)]
to generate fn new
that is annotated with #[builder]
.
The generated constructor will have private visibility and the builder will match the visibility of the struct.
Motivation
The difference between this and other builder crates is that constructors/methods can be used to derive builders rather than structs. This results in a more natural fit with regular Rust code, and no annotation magic to define behavior.
Advantages:
- You can specify fields in your constructor that do not appear in your struct.
- No magic to default values, just use an
Option
param in yourfn
and default as normal. async
constructors derivesasync
builders.- Fallible constructors (
Result
) derives fallible builders. - Special
Vec
,Deque
,Heap
,Set
,Map
support. Add single or multiple items. - Generated builders can have receiver,
self
,&self
and&mut self
are supported.
This crate is heavily inspired by the excellent typed-builder crate. It is a good alternative to this crate and well worth considering.
Recipes
All of these recipes and more can be found in the examples directory
Just write your rust code as usual and annotate the constructor impl with [builder]
Constructors
Builders can be generated on methods that have no receiver.
Configuration:
entry
defaults based onfn
name:new
=>builder
<name>_new
=><name>_builder
<anything_else>
=> cannot be defaulted and must be specified.
exit
defaults tobuild
Methods
Builders can be generated on methods that take self
, &self
and &mut self
as a parameter.
Configuration:
entry
cannot be defaulted and must be specified.exit
defaults tocall
use buildstructor;
;
Optional field
Fields that are Option
will also be optional in the builder. You should do defaulting in your constructor.
Note that if a field is an Option
or collection then if a user forgets to set it a compile error will be generated.
Into field
Simple types
Types automatically have into conversion if:
- the type is not a scalar.
- the type has no generic parameters. (this may be relaxed later)
- the type is a generic parameter from the impl or constructor method.
This is useful for Strings, but also other types where you want to overload the singular build method. Create an enum that derives From for all the types you want to support and then use this type in your constructor.
Complex types
You can use generics as usual in your constructor. However, this has the downside of not being able to support optional fields.
Async
To create an async
builder just make your constructor async
.
async
Fallible
To create a fallible builder just make your constructor fallible using Result
.
use Error;
Collections and maps
Collections and maps are given special treatment, the builder will add additional methods to build the collection one element at a time.
Supported types
Collections are matched by type name:
Type Name | Method used to insert |
---|---|
...Buffer | push(_) |
...Deque | push(_) |
...Heap | push(_) |
...Set | insert(_) |
...Stack | push(_) |
...Map | insert(_, _) |
Vec | push(_) |
If your type does not conform to these patterns then you can use a type alias to trick Buildstructor into giving the parameter special treatment.
Naming
Use the plural form in your constructor argument and buildstructor
will automatically try to figure out the singular form for individual entry. For instance:
addresses
=> address
In the case that a singular form cannot be derived automatically the suffix _entry
will be used. For instance:
frodo
=> frodo_entry
Into
Adding a singular entry will automatically perform an into conversion if:
- the type is not a scalar.
- the type has no generic parameters. (this may be relaxed later)
- the type is a generic parameter from the impl or constructor method.
This is useful for Strings, but also other types where you want to overload the singular build method. Create an enum that derives From for all the types you want to support and then use this type in your constructor.
There had to be some magic somewhere.
Visibility
Builders will automatically inherit the visibility of the method that they are decorating. However, if you want to override this then you can use the visibility.
This is useful if you want Buildstructor builders to be the sole entry point for creating your struct.
use Error;
Upgrade to 0.2.0
To provide more control over generated builders and allow builders for methods with receivers the top level annotation has changed:
#[buildstructor::builder]
=> #[buildstructor::buildstructor]
- Annotate the impl with:
#[buildstructor::buildstructor]
- Annotate methods to create a builders for with:
#[builder]
TODO
- Better error messages.
- More testing.
PRs welcome!
License
Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.